import { getTrainInfo, createTrain, updateTrain } from '@/api/campus/train'
export default {
  props: [],
  data() {
    return {
      loading: false,
      visible: false,
      campusRelationListVisible: false,
      isDetail: false,
      dataForm: {
        id: '',
        title: undefined,
        typeId: undefined,
        trainDate: undefined,
        nper: 0,
        certificateTemplate: undefined,
        enabledMark: 1,
        brand: undefined
      },
      rules: {
        title: [
          {
            required: true,
            message: '请输入师训标题',
            trigger: 'blur'
          }
        ],
        brand: [
          {
            required: true,
            message: '请选择师训品牌',
            trigger: 'change'
          }
        ],
        certificateTemplate: [
          {
            required: true,
            message: '请选择师训类型',
            trigger: 'change'
          }
        ],
        typeId: [
          {
            required: true,
            message: '请选择证书模版',
            trigger: 'change'
          }
        ],
        trainDate: [
          {
            required: true,
            message: '请输入师训日期',
            trigger: 'change'
          }
        ],
        nper: [
          {
            required: true,
            message: '请输入师训期数',
            trigger: ['blur', 'change']
          }
        ]
      },
      brandOptions: [],
      typeIdOptions: [],
      certificateTemplateOptions: []
    }
  },
  computed: {},
  watch: {},
  created() {
    this.$store.dispatch('base/getDictionaryData', { sort: 'TrainType' }).then(res => {
      this.typeIdOptions = res.filter(t => t.enabledMark == 1)
    })
    this.$store.dispatch('base/getDictionaryData', { sort: 'TrainCertificateType' }).then(res => {
      this.certificateTemplateOptions = res.filter(t => t.enabledMark == 1)
    })
    this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
      this.brandOptions = res
    })
  },
  mounted() {},
  methods: {
    goBack() {
      this.$emit('refresh')
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0
      this.visible = true
      this.isDetail = isDetail || false
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields()
        if (this.dataForm.id) {
          getTrainInfo(this.dataForm.id).then(res => {
            this.dataForm = res.data
            if (!this.dataForm.picture) this.dataForm.picture = []
          })
        }
      })
    },
    dataFormSubmit() {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          this.loading = true
          const formMethod = this.dataForm.id ? updateTrain : createTrain
          formMethod(this.dataForm).then(res => {
            this.loading = false
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.visible = false
                this.$emit('refresh', true)
              }
            })
          })
        }
      })
    },
    closeDialog() {
      this.$refs['elForm'].resetFields()
    },

    handleDelExtForm(index) {
      this.dataForm.formJson.splice(index, 1)
    },
    addHandleExtForm() {
      let item = {
        label: undefined,
        type: undefined,
        required: undefined,
        defaultValue: undefined,
        options: undefined,
        placeholder: undefined
      }
      if (this.dataForm.formJson == null) {
        this.dataForm.formJson = []
      }
      this.dataForm.formJson.push(item)
    }
  }
}
